var gaoluSmall=[
    {name:'history',type:'line',lineStyle:'solid'},
    {name:'prediction',type:'line',lineStyle:'dotted'}
];
var gaoluBig=[
    {name:'gaolu1_history',type:'line',lineStyle:'solid',color:'#01c5f7'},
    {name:'gaolu1_forecast',type:'line',lineStyle:'dotted', color:'#01c5f7'},
    {name:'gaolu2_history',type:'line',lineStyle:'solid', color:'#8c04c6'},
    {name:'gaolu2_forecast',type:'line',lineStyle:'dotted', color:'#8c04c6'},
    {name:'gaolu3_history',type:'line',lineStyle:'solid', color:'#6822e4'},
    {name:'gaolu3_forecast',type:'line',lineStyle:'dotted',color:'#6822e4'},
    {name:'gaolu4_history',type:'line',lineStyle:'solid',color:'#e8830d'},
    {name:'gaolu4_forecast',type:'line',lineStyle:'dotted',color:'#e8830d'},
    {name:'gaolu5_history',type:'line',lineStyle:'solid',color:'#87c313'},
    {name:'gaolu5_forecast',type:'line',lineStyle:'dotted',color:'#87c313'},
    {name:'SUM_history',type:'line',lineStyle:'solid',color:'#1b8dff'},
    {name:'SUM_forecast',type:'line',lineStyle:'dotted',color:'#1b8dff'}
];
var tootipJson = {
    'gaolu1_history':'高炉1实际',
    'gaolu1_forecast':'高炉1预测',
    'gaolu2_history':'高炉2实际',
    'gaolu2_forecast':'高炉2预测',
    'gaolu3_history':'高炉3实际',
    'gaolu3_forecast':'高炉3预测',
    'gaolu4_history':'高炉4实际',
    'gaolu4_forecast':'高炉4预测',
    'gaolu5_history':'高炉5实际',
    'gaolu5_forecast':'高炉5预测',
    'SUM_history':'高炉总的实际',
    'SUM_forecast':'高炉总的预测'
};
var gaoluBigoption = {
    tooltip:{
        show:true,
        z:100,
        formatter:function(params){
            var nameArr = [];
            var valueArr = [];
            var str = '';
            for(var i=0;i<params.length;i++){
                nameArr[i]=tootipJson[params[i].seriesName];
                valueArr[i]=Number(params[i].data).toFixed(1);
                str+= nameArr[i]+' : '+valueArr[i]+'<br/>'
            }
            return str
        }
    }
};
var gaoluSmallOption={
    tooltip:{
        show:true,
        z:-1,
        formatter:function(params){
            var valueArr = [];
            var str = '';
            var nameArr = ['预测值','实际值'];
            for(var i=0;i<params.length;i++){
                valueArr[i]=Number(params[i].data).toFixed(1);
                str+= nameArr[i]+' : '+valueArr[i]+'<br/>'
            }
            return str
        }
    },
    grid:{
        left:'15%',
        right:0,
    },
    yAxis:[{
        type:'value',
        name:'X10⁵',
        axisLabel:{
            show:true,
            inside:false,
            align:'',
            color:'#b0e9fe',
            fontSize:12,
            formatter:function (value, index) {
               var val=(value/Math.pow(10,5)).toFixed(1);
               return val;
            }
        }
        
    }]
}

//获取所有小图元素
var blastGroups=$('.main-item-chart');
var blastCollection=[];
//高炉集合
var gaoluData=Object.keys(blastIdCollection);
// 历史集合
var historyData=getIdCollection(blastIdCollection,'gas_produce');
//预测集合
var forecastData=getIdCollection(blastIdCollection,'gas_prediction');
//基线集合
var baseLineData=getIdCollection(blastIdCollection,'gas_baseline');

//初始化5个高炉
blastGroups.each(function(index,obj){
    //为每个chart设置id
    var chartId=uuid(8,64);
    blastGroups.eq(index).attr('id',chartId);
    blastGroups.eq(index).attr('data-gas-produce',historyData[index]);
    blastGroups.eq(index).attr('data-gas-prediction',forecastData[index]);
    blastGroups.eq(index).attr('data-gas-baseline',baseLineData[index]);
    blastGroups.eq(index).attr('data-name',gaoluData[index]);
    blastCollection.push(new Chart(chartId,{
        type:'line',
        data:gaoluSmall,
        baseOption:gaoluSmallOption
    }));
});


//加载预测数据3min
gaoluSmallPrediction();
setInterval(function(){
    gaoluSmallPrediction(); 
},1000*60*1);

//加载历史数据5s
var saveHistory=new Collection();
gaoluSmallHistory();

//加载实时历史数据
setInterval(function(){
    gaoluSmallRealHistory();
},5000);

//基线 页面刷新
gaoluSmallBaseline();


//高炉状态
/*当前值的元素*/
var currentDatas=$('.current-data');
//预测值的元素
var forecastDatas=$('.prediction-data');
//背景图的颜色
var statusBg=$('.main-item-data');
setInterval(function(){
   gaoluStatus()
},5000);


//获取元素
var viewClose=$('#main_container_active_view_close');
var gaoluActive=$('.gaolu-active-legend-des');
//实例化blastName集合
var blastNames=new Collection();
//实例化大图
var gaoluChart=new Chart('gaolu_chart',{
    type:'line',
    data:gaoluBig,
    baseOption:gaoluBigoption

});

//大图交互
//点击小图
blastGroups.on('click',function(){
    //获取数据
    var _this=this;
    var attrs=getAttrs(_this,[
            'data-name',
            'data-gas-produce',
            'data-gas-prediction',
            'data-gas-baseline',
        ]);
    attrs['data-gas-produce'].push(historyData.slice(-1)[0]);
    attrs['data-gas-prediction'].push(forecastData.slice(-1)[0]);
    attrs['data-gas-baseline'].push(baseLineData.slice(-1)[0]);
    var index=$(this).attr('data-index');
    var name=attrs['data-name'];
    toUp(index);
    blastNames.addEl(name[0],name[0]);
    blastNames.addEl(name[1],name[1]);
    //console.log(blastNames.getKeys());
    //加载历史数据
    gaoluClickHistory(attrs,index);
    //加载预测数据
    gaoluClickPrediction(attrs,index);
    //加载基线
    gaoluClickBaseline(attrs,index);
});

//关闭大图
viewClose.on('click',function(){
    //清空高炉名字数组
    blastNames.emptyEl();
    //大图向下隐藏
    toDown();
    //重新实例化大图，目前清空所有数据
    gaoluChart=new Chart('gaolu_chart',{
        type:'line',
        data:gaoluBig,
        baseOption:gaoluBigoption
    });
});

//为大图的legend设置属性
gaoluActive.each(function(index,obj){
    //为每个chart设置属性
    var obj=$(obj);
    obj.attr('data-gas-produce',historyData[index]);
    obj.attr('data-gas-prediction',forecastData[index]);
    obj.attr('data-gas-baseline',baseLineData[index]);
    obj.attr('data-name',gaoluData[index]);
    obj.attr('data-index',index);   
});

/*大图图例控制加载不同高炉的线条*/
gaoluActive.on('click',function(){
    var _this=this;
    var attrs=getAttrs(_this,[
            'data-name',
            'data-index',
            'data-gas-produce',
            'data-gas-prediction',
            'data-gas-baseline',
            'data-selected'
        ]);
    var name=attrs['data-name'];
    var index=attrs['data-index'];
    var status=attrs['data-selected'];
    if(status==1){
        $(this).attr('data-selected',0);
        $(this).find('.gaolu-legend-icon').removeClass('gaolu-legend-icon--active');
        blastNames.deleteEl(name);
        if(name=='gas_total'){
            gaoluChart.loadData('SUM_history',[]);
            gaoluChart.loadData('SUM_forecast',[]);
        }else{
            gaoluChart.loadData(name+'_history',[]);
            gaoluChart.loadData(name+'_forecast',[]);
            //gaoluChart.loadBaseLine(name+'_history',{show:false});
        }
    }else{
        $(this).attr('data-selected',1);
        $(this).find('.gaolu-legend-icon').addClass('gaolu-legend-icon--active');
        blastNames.addEl(name,name);
        //交互
        //加载历史数据
        gaoluChart.LoadingShow();
        gaoluLegendHistory(attrs,index,name);
        //加载预测数据
        gaoluLegendPrediction(attrs,index,name);
        //加载基线
        //gaoluLegendBaseline(attrs,index,name);
    }
});


//vue新增代码
//消息中心
var gaolu_vm=new Vue({
    el:'#main_content',
    data:{
        msgCenter:[],
        msgType:'all',
        srcline:'history.html'
    },
    mounted:function(){
        this.$nextTick(function(){
            var _this=this;
            setInterval(function(){
                _this.getMsgCenter();
            },3000);
        });
    },
    methods:{
        getMsgCenter:function(){
            var _this=this;
            bus.$on('msgType',function(val){
                _this.msgType=val;
            });
            var success=function(res){
                //_this.msgCenter=eval('('+res.data+')');
                _this.msgCenter=res.data;
            };
            var error=function(e){
                console.log('高炉消息中心出错了');
            };
            this.$http(URL+'/message/current').then(success,error);

        }
    }
});

//头部
var header_vm=new Vue({
    el:'#header'
});
//左侧部分
var left_vm=new Vue({
    el:'#left_side'
});

//产气归因
Chart3('gas_chart',URL+'/coalgas/gaolu/bodongguiyin');
//向上
function toUp(index){
    $('.main-container-active').animate({'top':'0px'});
    gaoluActive.attr('data-selected',0);
    gaoluActive.eq(index).attr('data-selected',1);
    gaoluActive.eq(5).attr('data-selected',1);
    $('.gaolu-legend-icon').removeClass('gaolu-legend-icon--active');
    gaoluActive.eq(index).find('.gaolu-legend-icon').addClass('gaolu-legend-icon--active')
    gaoluActive.eq(5).find('.gaolu-legend-icon').addClass('gaolu-legend-icon--active')
}
//向下
function toDown(){
    $('.main-container-active').animate({'top':'582px'});
}


/*******************ajax交互********************/

//高炉小图加载历史数据
function gaoluSmallHistory(){
    $.ajax({
        url:URL+'/blast/history',
        //url:'../data/history3.json',
        type:'post',
        data:{
            'blastGroupId':historyData.slice(0,-1).toString(),
            'history':3600*Math.pow(10,3)
        },
        success:function(data){
            for(var i=0;i<historyData.length-1;i++){
                var obj=data[historyData[i]];
                var Axis=getAxis(obj);
                //console.log('历史'+formatterTime(Axis.dataX[0]));
                saveHistory.addEl(historyData[i],Axis.dataY);
                blastCollection[i].loadData('history',Axis.dataY);
                blastCollection[i].showTip('history',Axis.dataX);
            }
        }
    });
}
//高炉小图实时更新历史数据
function gaoluSmallRealHistory(){
    $.ajax({
        url:URL+'/blast/realPoint',
        type:'post',
        data:{
            'blastGroupId':historyData.slice(0,-1).toString()
        },
        success:function(data){
            var upDateHistory=saveHistory.getEls();
            for(var i=0;i<historyData.length-1;i++){
                var hisName=historyData[i];
                upDateHistory[hisName].shift();
                upDateHistory[hisName].push(data[hisName][1])
                var data1=[];
                var data1X=[];
                for(var j=0;j<upDateHistory[hisName].length;j++){
                    data1[j]=upDateHistory[hisName][j];
                    data1X[j]=data[hisName][0];
                }
                //console.log('历史实时'+formatterTime(data1X[0]));
                blastCollection[i].loadData('history',data1);
                blastCollection[i].showTip('history',data1X);
            }
        },
        error:function(e){
            console.log(e);
        }
    });
}

//高炉小图预测数据
function gaoluSmallPrediction(){
    $.ajax({
        url:URL+'/blast/prediction',
        //url:'../data/prediction.json',
        //dataType:'text',
        type:'post',
        data:{
            'blastGroupId':forecastData.slice(0,-1).toString(),
            'history':3600*Math.pow(10,3)
        },
        success:function(data){
            var data=data;
            //var data=eval('('+data+')');
            for(var i=0;i<forecastData.length-1;i++){
                var obj=data[forecastData[i]];
                var Axis=getAxis(obj);
                blastCollection[i].loadX(Axis.dataX);
                blastCollection[i].loadData('prediction',Axis.dataY);
            }  
        },
        error:function(e){
            console.log(e);
        }
    }); 
}

//高炉小图基线
function gaoluSmallBaseline(){
    $.ajax({
        url:URL+'/blast/baseLine',
        //url:'../data/baseline.json',
        //dataType:'text',
        type:'post',
        data:{
            ids:baseLineData.slice(0,-1).toString()
        },
        success:function(data){
            //console.log(data);
            //var baseLine=eval('('+data+')');
            var baseLine=data;
            for(var i=0;i<baseLineData.length-1;i++){
                var obj=baseLine[baseLineData[i]];
                blastCollection[i].loadBaseLine('history',{data:obj})
            }
        }
    });
}

//高炉状态
function gaoluStatus(){
    $.ajax({
        url:URL+'/deviceInfo/gaolu',
        type:'post',
        data:{
            blastName:gaoluData.slice(0,-1).toString()
        },
        success:function(data){
            var currentJson=data;
            var len=gaoluData.length-1;
            $.each(currentDatas,function(index,obj){
                var name=gaoluData[index%len];
                var originCurrentData=currentJson[name]['currentGas'];
                var formatterCurrentData=(originCurrentData/ Math.pow(10,5)).toFixed(2);
                $(obj).text(formatterCurrentData);
              
            });
            $.each(forecastDatas,function(index,obj){
                var name=gaoluData[index%len];
                var originForecastData=currentJson[name]['nextPredict'];
                var formatterForecastData=(originForecastData/ Math.pow(10,5)).toFixed(2);
                $(obj).text(formatterForecastData);
            });
            //main-item-normal 正常
            //main-item-reduce 减风
            //main-item-mod 休风
            $.each(statusBg,function(index,obj){
                var name=gaoluData[index];
                var status=currentJson[name]['status'];
                if(status==0){//正常
                    $(obj).removeClass('main-item-reduce').removeClass('main-item-mod').addClass('main-item-normal');
                }else if(status==1){//减风
                    $(obj).removeClass('main-item-normal').removeClass('main-item-mod').addClass('main-item-reduce');
                }else if(status==2){//休风
                    $(obj).removeClass('main-item-reduce').removeClass('main-item-normal').addClass('main-item-mod');
                }else{
                    console.log('高炉状态为'+status);
                }
            })

            
        },
        error:function(e){
            console.log('高炉状态加载出错了');
        }
   }); 
}
/*点击小图交互*/
//点击小图加载历史
function gaoluClickHistory(attrs,index){
    $.ajax({
        url:URL+'/blast/history',
        type:'post',
        data:{
            blastGroupId:attrs['data-gas-produce'].toString(),
            history:3600*Math.pow(10,3)
        },
        success:function(data){
            var dataJson=data;
            //历史的
            var realData=dataJson[historyData[index]];
            var axisHistory=getAxis(realData);
            var num=Number(index)+1;
            gaoluChart.loadData('gaolu'+num+'_history',axisHistory.dataY);
            //总
            var realTotal=dataJson[historyData[historyData.length-1]];
            var axisTotal=getAxis(realTotal);
            gaoluChart.loadData('SUM_history',axisTotal.dataY);
        },
        error:function(e){
            console.log(e);
        }
    });
}
//点击小图加载预测
function gaoluClickPrediction(attrs,index){
    $.ajax({
        url:URL+'/blast/prediction',
        data:{
            blastGroupId:attrs['data-gas-prediction'].toString(),
            history:3600*Math.pow(10,3)
        },
        success:function(data){
            var dataJson=data;
            //预测的
            var foreData=dataJson[forecastData[index]];
            var axisPrediction=getAxis(foreData);
            var num=Number(index)+1;
            gaoluChart.loadX(axisPrediction.dataX);
            gaoluChart.loadData('gaolu'+num+'_forecast',axisPrediction.dataY);

            //总预测
            var realTotal=dataJson[forecastData[forecastData.length-1]];
            var dataTotal=[];
            for(var i=0;i<realTotal.length;i++){
                dataTotal[i]=realTotal[i][1];
            }
            gaoluChart.loadData('SUM_forecast',dataTotal);
        },
        error:function(e){
            console.log(e);
        }
    });
}
//点击小图加载基线
function gaoluClickBaseline(attrs,index){
    $.ajax({
        url:URL+'/blast/baseLine',
        type:'post',
        data:{
            ids:attrs['data-gas-baseline'].toString()
        },
        success:function(data){
            var baseLine=data;
            //console.log(baseLine);
            var num=Number(index)+1;
            var line=baseLine[baseLineData[index]];
            var sum=baseLine[baseLineData[baseLineData.length-1]];
            gaoluChart.loadBaseLine('gaolu'+num+'_history',{data:line})
            
           
            gaoluChart.loadBaseLine('SUM_history',{data:sum})
        },
        error:function(e){
            console.log(e);
        }
    });
}

/*点击图例交互*/
//点击图例加载历史
function gaoluLegendHistory(attrs,index,name){
    $.ajax({
        url:URL+'/blast/history',
        type:'post',
        data:{
            blastGroupId:attrs['data-gas-produce'].toString(),
            history:3600*Math.pow(10,3)
        },
        success:function(data){
            var dataJson=data;
            var obj=dataJson[historyData[index]];
            var axisHistory=getAxis(obj);
            if(name=='gas_total'){
                gaoluChart.loadData('SUM_history',axisHistory.dataY);
            }else{
                gaoluChart.loadData(name+'_history',axisHistory.dataY);
            }    
        },
        error:function(e){
            console.log(e);
        }
    });
}
//点击图例加载预测
function gaoluLegendPrediction(attrs,index,name){
    $.ajax({
        url:URL+'/blast/prediction',
        data:{
            blastGroupId:attrs['data-gas-prediction'].toString(),
            history:3600*Math.pow(10,3)
        },
        success:function(data){
            var dataJson=data;
            var num=Number(index)+1;
            var foreData=dataJson[forecastData[index]];
            var axisPrediction=getAxis(foreData);
            gaoluChart.loadData(name+'_forecast',axisPrediction.dataY);
            if(name =='gas_total'){
                //alert('暂时无预测数据');
                //return false;
                //
                gaoluChart.loadX(axisPrediction.dataX);
                gaoluChart.loadData('SUM_forecast',axisPrediction.dataY);
            }else{
                gaoluChart.loadX(axisPrediction.dataX);
                gaoluChart.loadData(name+'_forecast',axisPrediction.dataY);
            }
        },
        error:function(e){
            console.log(e);
        }
    });
}
//点击图例加载基线
function gaoluLegendBaseline(attrs,index,name){
    $.ajax({
        //url:'../data/baseline2.json',
        url:URL+'/blast/baseLine',
        type:'post',
        //dataType:'text',
        data:{
            ids:attrs['data-gas-baseline'].toString()
        },
        success:function(data){
            var baseLine=data;
            if(name=="gas_total"){
                // alert('暂无基线数据');
                // return false;
                gaoluChart.loadBaseLine('SUM_history',{data:baseLine})
            }else{
                gaoluChart.loadBaseLine(name+'_history',{data:baseLine});
            }
        },
        error:function(e){
            console.log(e);
        }
    });
}






